#pragma once

#include <vtkInteractorStyle.h>
#include <IVtkTools_ShapeDataSource.hxx>
#include <IVtkTools_ShapeObject.hxx>
#include <IVtkTools_ShapePicker.hxx>

#include "vtkSmartPointer.h"
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkProperty.h>
#include <vtkRendererCollection.h>

// VIS includes
#include <IVtkOCC_Shape.hxx>
#include <IVtkTools_DisplayModeFilter.hxx>
#include <IVtkTools_SubPolyDataFilter.hxx>

#include "QIVtkSelectionPipeline.h"
#include "QTypes.h"
#include <set>

VTK_ABI_NAMESPACE_BEGIN

class CustomActor;

enum class EViewStytle : UINT8
{
	frontView,
    backView,
    topView,
    bottomView,
    leftView,
    rightView,
	positiveTriaxialView,
};

class WindowInteractorStyle : public vtkInteractorStyle
{
	friend class MainWindow;
public:
	static WindowInteractorStyle* New();
	vtkTypeMacro(WindowInteractorStyle, vtkInteractorStyle);

protected:
	WindowInteractorStyle();
	~WindowInteractorStyle() override;
//----------------------Override Begin---------------------------------//
	virtual void Dolly(double factor);
	virtual void OnChar() override;

	virtual void OnMouseMove() override;
	virtual void OnLeftButtonDown() override;
	virtual void OnLeftButtonUp() override;
	virtual void OnMiddleButtonDown() override;
	virtual void OnMiddleButtonUp() override;
	virtual void OnRightButtonDown() override;
	virtual void OnRightButtonUp() override;
	virtual void OnMouseWheelForward() override;
	virtual void OnMouseWheelBackward() override;

	virtual void Dolly() override;
	virtual void Pan() override;
	virtual void Rotate() override;
//----------------------Override End-----------------------------------//

public:
	void GetVertexPickerTargetPos(bool& bIsPick, gp_Pnt& Pos);
	void SetMaxSelectedActors(uint maxSelectedActors);
	void ResetMaxSelectedActors();
	void SelectActor(Handle(QIVtkSelectionPipeline) pipeline, bool bClearSelected = false);

	// 视图切换
	void SetStandardView(vtkRenderer* renderer, EViewStytle viewStytle);
	void SetFitView(vtkRenderer* renderer);

	// 切换操作交互模式为2D（仅平移，不能旋转）—— 用于草图
	void Toggle2DMode(const gp_Pnt& focusCenter, const gp_Dir& N, const gp_Dir& R);
	void ResetMode();

public:
	class MainWindow* m_mainWindow;
	std::set<EVertexType> _selectedVertexTypes;
protected:

	double MotionFactor;
	Handle(QIVtkSelectionPipeline) HighlightActor;
	std::vector<Handle(QIVtkSelectionPipeline)> selectActors;

	vtkSmartPointer<vtkActor> tempSphereActor;

	uint _defaultMaxSelectedActors = 100;
	uint _maxSelectedActors = 100;

	// 相机参数，用于还原视角
	gp_Pnt _lastCameraPos;
	gp_Dir _lastCameraViewUp;
	gp_Pnt _lastFocalPoint;
	bool bIsIn2DMode = false;

//--------------------------------QVTKInteractorStytle Start-----------------------------//

public:
	// 对所有的Actor设置select mode
	void setSelectionMode(IVtk_SelectionMode mode);
	// 是否可以选取多个
	void setCanMultiSelect(bool bCanMultiPick);
	// 对特定的Actor设置select mode
	//void setSelectionModeForActor(Handle(QIVtkSelectionPipeline) actor, IVtk_SelectionMode mode);

	void OnSelection(Standard_Boolean appendId);
	void MoveTo(Standard_Integer theX, Standard_Integer theY);
	void setVTKRenderWindow(vtkRenderWindow* vtkRenderWindow);
	void setRenderer(const vtkSmartPointer<vtkRenderer>& theRenderer);
	void setOcctPicker(const vtkSmartPointer<IVtkTools_ShapePicker>& thePicker);

	void clearShapeMaps();
	/**
	* @brief Adds a selection pipeline to the interactor style.
	* @param pipeline A handle to the QIVtkSelectionPipeline.
	* @param id The ID associated with the pipeline.
	*/
	void addPipeline(const Handle(QIVtkSelectionPipeline) pipeline, IVtk_IdType id);
	void removePipeline(IVtk_IdType id);

	void ClearSelectedActors();
	void RemoveSelectedActor(Handle(QIVtkSelectionPipeline) pipeline);
private:
	bool _coordPickerEnabled = false;
	bool _occtPickerEnabled = false;
	bool _planePickerEnabled = false;
	bool _canMultiSelect = true;
public:
	/*
	 *functions to set multiPickers.
	 */

	void setOcctPickerEnabled(bool isEnabled) { _occtPickerEnabled = isEnabled; }
	void setCoordPickerEnabled(bool isEnabled) { _coordPickerEnabled = isEnabled; }
	void setPlanePickerEnabled(bool isEnabled) { _planePickerEnabled = isEnabled; }

private:
	vtkActor* LastPickedActor = nullptr;
	vtkSmartPointer<vtkActor> edgesActor;
	vtkSmartPointer<vtkProperty> LastPickedProperty = vtkProperty::New();

public:
	// ! Getter and Setters
	[[nodiscard]] vtkSmartPointer<vtkRenderer> getRenderer() const
	{
		return _renderer;
	}

	[[nodiscard]] vtkSmartPointer<IVtkTools_ShapePicker> getOcctPicker() const
	{
		return _occtpicker;
	}

	[[nodiscard]] Handle(TopoDS_Shape) getSelectedShapes() const
	{
		return _selectedShape;
	}

	[[nodiscard]] NCollection_DataMap<IVtk_IdType, Handle(QIVtkSelectionPipeline)> getShapePipelinesMap() const
	{
		return _shapePipelinesMap;
	}

private:
	// ! Smart pointer to the shape picker.
	vtkSmartPointer<IVtkTools_ShapePicker> _occtpicker;
	// ! Smart pointer to the VTK renderer.
	vtkSmartPointer<vtkRenderer> _renderer;

	// ! Pointer to the QVTK render window.
	vtkRenderWindow* _qvtkRenderWindow = nullptr;

	//Shapes Selected.
	Handle(TopoDS_Shape) _selectedShape;

	int lastPos[2]{ 0, 0 };

	// ! Current selection mode.
	IVtk_SelectionMode _currentSelectionMode = SM_None;

	//存储要渲染的pipelines
	NCollection_DataMap<IVtk_IdType, Handle(QIVtkSelectionPipeline)> _shapePipelinesMap;

	//Shapes Selected. 一个actor的
	std::vector<std::reference_wrapper<const TopoDS_Shape>> _selectedShapes;

	// ! Map of selected sub-shape IDs.
	NCollection_DataMap<IVtk_IdType, IVtk_ShapeIdList*> _selectedSubShapeIdsMap;
//--------------------------------QVTKInteractorStytle End-----------------------------//
};
VTK_ABI_NAMESPACE_END